package com.next

import grails.transaction.Transactional

/**
 * @Author 班旭娟
 * @CreatedDate 2017-4-21
 * @ModifiedDate 2017-11-2
 */

@Transactional
class WorkflowInstanceNotificationService
{

    static scope = "singleton"
    //短信
    MessageService messageService

    def initWorkflowInstanceNotification(WorkflowInstanceStage workflowInstanceStage, WorkflowStage workflowStage)
    {
        def workflowInstanceNotification
        def userPositionList
        def workflowNotifications = WorkflowNotification.findAll("from WorkflowNotification where stage.id = ${workflowStage?.id}")
        workflowNotifications?.each { workflowNotification ->
            if (workflowNotification?.position)
            {
                userPositionList = UserPosition.findAll("from UserPosition where position.id = :position", [position: workflowNotification?.position.id])
                userPositionList?.each { userPosition ->
                    workflowInstanceNotification = WorkflowInstanceNotification.find("from WorkflowInstanceNotification where user.id = ${userPosition?.user?.id} and stage.id = ${workflowInstanceStage?.id} and messageTemplate.id = ${workflowNotification?.messageTemplate?.id} and cellphone = '${workflowNotification?.cellphone}'")
                    if (!workflowInstanceNotification)
                    {
                        workflowInstanceNotification = new WorkflowInstanceNotification()
                        workflowInstanceNotification.stage = workflowInstanceStage
                        workflowInstanceNotification.user = userPosition?.user
                        workflowInstanceNotification.messageTemplate = workflowNotification?.messageTemplate
                        workflowInstanceNotification.toManager = workflowNotification?.toManager
                        workflowInstanceNotification.cellphone = workflowNotification?.cellphone
                        workflowInstanceNotification.save flush: true
                    }
                }
            }
            else
            {
                workflowInstanceNotification = WorkflowInstanceNotification.find("from WorkflowInstanceNotification where user.id is null and stage.id = ${workflowInstanceStage?.id} and messageTemplate.id = ${workflowNotification?.messageTemplate?.id} and cellphone = '${workflowNotification?.cellphone}'")
                if (!workflowInstanceNotification)
                {
                    workflowInstanceNotification = new WorkflowInstanceNotification()
                    workflowInstanceNotification.stage = workflowInstanceStage
                    workflowInstanceNotification.messageTemplate = workflowNotification?.messageTemplate
                    workflowInstanceNotification.toManager = workflowNotification?.toManager
                    workflowInstanceNotification.cellphone = workflowNotification?.cellphone
                    workflowInstanceNotification.save flush: true
                }
            }
        }
    }

    def sendNotification(WorkflowInstance workflowInstance)
    {
        def message
        def cellphone
        def email
        def manager

        def shell = new GroovyShell()
        def closure

        if (workflowInstance?.status?.name == "Pending")
        {
            def workflowInstanceNotifications = WorkflowInstanceNotification.findAll("from WorkflowInstanceNotification where stage.id = ${workflowInstance?.stage?.id}")
            workflowInstanceNotifications.each {
                try
                {
                    closure = shell.evaluate(it?.messageTemplate?.template)
                    message = closure(workflowInstance)

                    if (it?.user)
                    {
                        cellphone = it?.user?.cellphone

                        //发短信
                        messageService.sendMessage2(cellphone, message)

                        //发邮件
                        email = it?.user?.email
                        if (email)
                        {
                            messageService.sendEmail(email, message)
                        }
                    }
                    else if (it?.cellphone)
                    {
                        closure = shell.evaluate(it?.cellphone)
                        cellphone = closure(workflowInstance)

                        //发短信
                        messageService.sendMessage2(cellphone, message)
                    }
                    else
                    {
                        log.error "workflowInstance cellphone configuration error!"
                    }

                    if (it?.toManager && it?.user)
                    {
                        manager = it?.user?.reportTo
                        while (manager)
                        {
                            cellphone = manager?.cellphone
                            messageService.sendMessage2(cellphone, message)

                            //发邮件
                            email = manager?.email
                            if (email)
                            {
                                messageService.sendEmail(email, message)
                            }

                            manager = manager?.reportTo
                        }
                    }
                }
                catch (Exception e)
                {
                    log.error "workflowInstance script execute error: " + e
                }
            }
        }
    }
}
